package cn.db101.jcc.crawl.processor;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.lang.UUID;
import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.URLUtil;
import lombok.val;
import org.springframework.util.ResourceUtils;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.processor.PageProcessor;
import us.codecraft.webmagic.selector.Html;

import java.io.File;
import java.io.FileNotFoundException;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.List;

import static us.codecraft.webmagic.selector.Selectors.xpath;

/**
 * @author zooy
 * @since 2024/6/11-12:32
 */
public class LineupProcessor implements PageProcessor {
    @Override
    public void process(Page page) {
        // 获取文章列表
        if (page.getUrl().get().contains("zhenrong") == true) {
            List<String> urlList = page.getHtml().xpath("//div[@class='newsbox']/div[@class='item']/a").all();
            urlList.forEach(url -> {
                String href = new Html(url).xpath("//a/@href").get();
                page.addTargetRequest(href);
            });

            page.setSkip(true);
        } else {
            // 获取文章内容
            List<String> contentList = page.getHtml().xpath("//div[@class='contentbox']/p/text()").all();

            for (int i = contentList.size() - 1; i >= 0; i--) {

                String pStr = contentList.get(i);
                if ("".equals(pStr) || "：".equals(pStr)) {
                    continue;
                }
                contentList.set(i, "<p>"+ pStr +"</p>");
            }

            page.putField("content", StrUtil.join("", contentList));
            page.putField("description", contentList.get(0));

            // 获取图片地址
            String url = page.getHtml().xpath("//img[@id='img0_1']/@src").get();
            // 文件url转成本地文件
            try {
                String name = UUID.fastUUID().toString(true) + ".jpg";
                long copy = IoUtil.copy(URLUtil.getStream(new URL(url)), FileUtil.getOutputStream("d:/lineup/" + name), IoUtil.DEFAULT_BUFFER_SIZE);
                page.putField("imageUrl", "https://wxapi.db101.cn/static/lineup/" + name);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }

            // 获取文章标题
            String title = page.getHtml().xpath("//h1[@class='tit']/span/text()").get();
            page.putField("title", title);

        }
    }

    @Override
    public Site getSite() {
        return Site.me().setRetryTimes(2).setSleepTime(1000);
    }
}
